package com.jucai.pppeiyouclient.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.bean.FriendListBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import io.rong.imkit.RongIM;

public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.ViewHolder> {

    private Activity mActivity;
    private ArrayList<FriendListBean> mArrayList;

    public FriendListAdapter(Activity activity, ArrayList<FriendListBean> arrayList) {
        this.mActivity = activity;
        this.mArrayList = arrayList;
    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        final FriendListBean friendListBean = mArrayList.get(position);

        if (TextCheckUtils.isEmpty(friendListBean.getMember_avatar())) {
            holder.avatarImageView.setImageResource(R.drawable.avatar);
        } else {
            Glide.with(mActivity)
                    .load(friendListBean.getMember_avatar())
                    .centerCrop()
                    .override(80, 80)
                    .into(holder.avatarImageView);
        }

        if (TextCheckUtils.isEmpty(friendListBean.getMember_nickname()) && TextCheckUtils.isEmpty(friendListBean.getMember_realname())) {
            holder.nicknameTextView.setText("神秘用户");
        } else {
            if (TextCheckUtils.isEmpty(friendListBean.getMember_nickname())) {
                holder.nicknameTextView.setText(friendListBean.getMember_realname());
            } else {
                holder.nicknameTextView.setText(friendListBean.getMember_nickname());
            }
        }

        holder.mainRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Public_Chat.getMemberChatId");
                param.put("target_member_id", friendListBean.getMember_id());

                CommonRequestNetData.getJsonObjectData(mActivity, param, true, new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        try {
                            String rongyun_id = jsonObject.getString("rongyun_id");
                            if (rongyun_id.equals("") || rongyun_id == null) {
                                ToastUtils.showToast("服务器异常，请稍后再试");
                                return;
                            }
                            String rongyun_avatar = jsonObject.getString("rongyun_avatar");
                            String rongyun_isfocus = jsonObject.getString("rongyun_isfocus");
                            String rongyun_nickname = jsonObject.getString("rongyun_nickname");
                            SharedPreferencesUtil.saveStringPreference(mActivity, "rongyun", "rongyun_isfocus", rongyun_isfocus);
                            SharedPreferencesUtil.saveStringPreference(mActivity, "rongyun", "rongyun_nickname", rongyun_nickname);
                            if (RongIM.getInstance() != null) RongIM.getInstance().startPrivateChat(mActivity, rongyun_id, rongyun_nickname);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                BaseApplication.targetMemberId = friendListBean.getMember_id();

            }
        });

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_friends_layout, group, false);
        return new ViewHolder(view);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout mainRelativeLayout;
        private ImageView avatarImageView;
        private TextView nicknameTextView;

        private ViewHolder(View view) {
            super(view);

            mainRelativeLayout = (RelativeLayout) view.findViewById(R.id.main_relative_layout);
            avatarImageView = (ImageView) view.findViewById(R.id.friend_img);
            nicknameTextView = (TextView) view.findViewById(R.id.friend_realname);

        }

    }

}
